<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Musaid_menus extends CI_Controller {

    var $santri_id;
    var $kasus_id;
    var $izin_id;
    var $status;
    var $cari;
    var $message;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->auth->restrict();

        $this->load->helper('text');
        $this->load->model(array('m_kasus', 'm_santri', 'm_kat_kasus', 'm_sub_kasus', 'm_izin'));
        $uri = $this->uri->uri_to_assoc(3);

        if (isset($uri['id'])) {
            $this->santri_id = $uri['id'];
        }

        if (isset($uri['status'])) {
            $this->status = $uri['status'];
        }

        if (isset($_POST['in_cari'])) {
            $this->cari = $this->input->post('in_cari');
            $this->session->set_userdata('cari', $this->cari);
        }

        if ($this->session->userdata('cari')) {
            $this->cari = $this->session->userdata('cari');
        }
    }

    public function santri($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_santri->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/musaid_menus/santri/page/';
        $config['total_rows'] = $this->m_santri->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_santri->num_pages = $config['per_page'];
        $this->m_santri->offset = $this->uri->segment(4);
        $this->m_santri->paginate = TRUE;
        $this->pagination->initialize($config);

        $santris = $this->m_santri->get();
        $data['title'] = $this->lang->line('menu_santri') . ' ' . $this->lang->line('webname');
        $data['santris'] = $santris;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('musaid_view_santri', $data);
        $this->load->view('view_footer', $data);
    }

    public function lihat_kasus($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_kasus->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/kasuss/index/page/';
        $config['total_rows'] = $this->m_kasus->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_kasus->num_pages = $config['per_page'];
        $this->m_kasus->offset = $this->uri->segment(4);
        $this->m_kasus->paginate = TRUE;
        $this->pagination->initialize($config);

        $kasuss = $this->m_kasus->get();
        $data['title'] = $this->lang->line('menu_kasus') . ' ' . $this->lang->line('webname');
        $data['kasuss'] = $kasuss;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('musaid_view_kasus', $data);
        $this->load->view('view_footer', $data);
    }

    public function kasus($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_tanggal', $this->lang->line('tanggal'), 'trim|required');
        $this->form_validation->set_rules('in_kat_kasus', $this->lang->line('kat_kasus'), 'trim|required');
        $this->form_validation->set_rules('in_sub_kasus', $this->lang->line('sub_kasus'), 'trim|required');
        $this->form_validation->set_rules('in_santri', $this->lang->line('santri'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $kat_kasuss = $this->m_kat_kasus->get_kategori_menu();
            $santris = $this->m_santri->get_kategori_menu();
            //$sub_kasuss = $this->m_sub_kasus->get_kategori_menu();

            $data['title'] = $this->lang->line('menu_kasus') . ' ' . $this->lang->line('webname');
            $data['kasus_id'] = $this->kasus_id;
            $data['kat_kasuss'] = $kat_kasuss;
            $data['santris'] = $santris;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('musaid_form_santri', $data);
            $this->load->view('view_footer_form_kasus_santri', $data);
        } else {
            $this->m_kasus->kasus_santri = $this->input->post('in_santri');
            $this->m_kasus->kasus_kat_kasus = $this->input->post('in_kat_kasus');
            $this->m_kasus->kasus_sub_kat_kasus = $this->input->post('in_sub_kasus');
            $this->m_kasus->kasus_keterangan = $this->input->post('in_keterangan');
            $this->m_kasus->kasus_status = $this->input->post('in_status');
            $this->m_kasus->kasus_tanggal = date('Y-m-d', strtotime($this->input->post('in_tanggal'))); //date('Y-m-d H:i:s');

            $this->m_kasus->kasus_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_kasus->kasus_dirubah = date('Y-m-d H:i:s');

            $this->m_kasus->kasus_id = '';
            $this->m_kasus->save();

            $this->lihat_kasus();
            //redirect('homes/', 'refresh');
        }
    }

    public function lihat_izin($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_izin->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/izins/index/page/';
        $config['total_rows'] = $this->m_izin->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_izin->num_pages = $config['per_page'];
        $this->m_izin->offset = $this->uri->segment(4);
        $this->m_izin->paginate = TRUE;
        $this->pagination->initialize($config);

        $izins = $this->m_izin->get();
        $data['title'] = $this->lang->line('menu_izin') . ' ' . $this->lang->line('webname');
        $data['izins'] = $izins;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('musaid_view_izin', $data);
        $this->load->view('view_footer', $data);
    }

    public function izin($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_tanggal_keluar', $this->lang->line('tanggal_keluar'), 'trim|required');
        $this->form_validation->set_rules('in_tanggal_masuk', $this->lang->line('tanggal_masuk'), 'trim|required');
        $this->form_validation->set_rules('in_santri', $this->lang->line('santri'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $santris = $this->m_santri->get_kategori_menu();
            //$sub_izins = $this->m_sub_izin->get_kategori_menu();

            $data['title'] = $this->lang->line('menu_izin') . ' ' . $this->lang->line('webname');
            $data['izin_id'] = $this->izin_id;
            $data['santris'] = $santris;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('musaid_form_izin', $data);
            $this->load->view('view_footer_izin', $data);
        } else {
            $this->m_izin->izin_santri = $this->input->post('in_santri');
            $this->m_izin->izin_masuk = date('Y-d-m H:i:s', strtotime($this->input->post('in_tanggal_masuk'))); //date('Y-m-d H:i:s');
            $this->m_izin->izin_keluar = date('Y-d-m H:i:s', strtotime($this->input->post('in_tanggal_keluar'))); //date('Y-m-d H:i:s');
            $this->m_izin->izin_keperluan = $this->input->post('in_keperluan');

            $this->m_izin->izin_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_izin->izin_dirubah = date('Y-m-d H:i:s');

            $this->m_izin->izin_id = '';
            $this->m_izin->save();

            $this->lihat_izin();
        }
    }

    public function get_sub_kasus_from_kat_id($kat) {
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($this->m_sub_kasus->get_sub_kasus_from_kat_kasus($kat)));
    }

}

/* End of file santris.php */
/* Location: ./application/controllers/santris.php */